<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use cmf\controller\HomeBaseController;
use think\Cache;
use think\Db;
use think\Request;

class IndexController extends HomeBaseController
{

    public function __construct(Request $request = null)
    {
        header("Content-type: text/html; charset=UTF-8");
        header('Access-Control-Allow-Origin:http://ytk.cn');
        parent::__construct($request);
//        if (!isset ($_SERVER['HTTP_X_WAP_PROFILE']))
//        {
//            exit(json_encode('检测到非法访问',JSON_UNESCAPED_UNICODE));
//        }

    }

    public function index()
    {
        $slide = Db::name('slide_item')
            ->where('slide_id', 1)
            ->where('status',1)
            ->select();

        $message = Db::name('portal_post')
            ->field('p1.*')
            ->alias('p1')
            ->join('portal_category_post p2', 'p1.id = p2.post_id and p2.category_id =1')
            ->select();

        $main_notice = Db::name('portal_post')
            ->alias('p1')
            ->field('p1.*,p2.category_id')
            ->join('portal_category_post p2','p1.id=p2.post_id and p1.recommended = 1')
            ->join('portal_category p3','p3.id=p2.category_id and p3.id=2')
            ->select();

        $main_notices =encode_data($main_notice);

        $study =  Db::name('portal_post')
            ->alias('p1')
            ->field('p1.*,p2.category_id')
            ->join('portal_category_post p2','p1.id=p2.post_id and p1.recommended = 1')
            ->join('portal_category p3','p3.id=p2.category_id and p3.id=3')
            ->select();
//        print_r(json_encode($study));
        $study = encode_data($study);

        $application = Db::name('portal_post')
            ->alias('p1')
            ->field('p1.*,p2.category_id')
            ->join('portal_category_post p2','p1.id=p2.post_id and p1.recommended = 1')
            ->join('portal_category p3','p3.id=p2.category_id and p3.id=4')
            ->select();
        $application = encode_data($application);
        $this->assign([
            'application' => $application,
            'study'     =>$study,
            'notice'   =>$main_notices,
            'slide' => $slide,
            'message' => $message,
        ]);

        return $this->fetch(':index');
    }

    public function ajax()
    {

        $arr        =  [];
        $time       =  date('Y/m/d  H:i', time());
        $open       =  $this->randomFloat(1, 100);
        $close      =  $this->randomFloat(1, 100);
        $rise_fall  =  $this->randomFloat(-100, 100);
        $rate       =  $this->randomFloat(0, 1);
        $hei        =  $this->randomFloat(1, 100);
        $low        =  $this->randomFloat(1, 100);
        $num        =  rand(100, 10000);
        $total = $this->randomFloat(100, 10000);

        array_push($arr, [$time, $open, $close, $rise_fall, $rate, $hei, $low, $num, $total, '-']);
        $data = ['time' => $time, 'open' => $open, 'close' => $close, 'rise_fall' => $rise_fall, 'highest' => $hei, 'lowest' => $low, 'num' => $num, 'total' => $total, 'rate' => $rate, 'add_time' => date('i', time()),'close_time'=> time()];
        Db::table('cmf_m_kline')->insert($data);

        echo json_encode($arr);
    }

    function randomFloat($min, $max)
    {
        $num = $min + mt_rand() / mt_getrandmax() * ($max - $min);
        return sprintf("%.2f", $num);
    }

    public function cache()
    {
        Cache::rm(1);
        Cache::rm(5);
        Cache::rm(15);
        Cache::rm(30);
        Cache::rm(60);

        var_dump(Cache::get(1));
    }

    public function k_setting()
    {
//       echo json_encode(input('kind'));
    }

    public function wave()
    {

        return $this->fetch(':wave');
    }

    public function kline()
    {
        $kind = input('kind');

        $result = Cache::get($kind);
        if ($result) {
            $data = Cache::get($kind);
        } else {
            $data = db('m_kline')
                ->where('add_time%' . $kind . '=0')
                ->select();
            $data = (array)$data;
            $data = array_values($data);
            $data = array_values($data[0]);
            $k_data = [];
            foreach ($data as $v) {
                $k_data[] = array_values($v);
            }
            $data = $k_data;
            $result = Cache::get($kind);
            if (!$result) {
                Cache::set($kind, $data, 60);
            }
        }
        echo json_encode($data);
    }
    public function pic(){

        return $this->fetch(':kline');
    }

    public function test(){
        $list = Db::name('portal_post')
            ->paginate('5');
        $page = $list->render();
        $test = $list->lastPage();
        $this->assign('list',$list);
        $this->assign('page',$page);
        $this->assign('num',$test);
        return $this->fetch(':test');
    }
    public function pull_fresh()
    {

    }
    public function down(){
        $list = Db::name('portal_post')
            ->select();
        exit(json_encode($list)) ;
    }

    public function api(){
//        exit(json_encode('PHP_test_data'));
//        var_dump($_SERVER['HTTP_X_WAP_PROFILE']);

    }
    public function test_login(){


        $username = input('username');
        $password = input('password');
        $arr = [
          'username' => $username,
          'password' => $password
        ];

        $result = Db::name('user')
            ->where('user_login',$username)
            ->find();
        $pass_result = cmf_compare_password($password,$result['user_pass']);

        if (!$result){
            exit( json_encode([
                'code' => 500,
                'msg'  => '用户不存在！'
            ],JSON_UNESCAPED_UNICODE ));
        }
        if ($pass_result){
            exit( json_encode([
                'code' => 200,
                'msg'  => '登录成功！',
                'username' => $result['user_login'],
                'pic'      => 'upload/'.$result['avatar']

            ],JSON_UNESCAPED_UNICODE));
        }else{
            exit( json_encode([
                'code' => 500,
                'msg'  => '密码错误！'
            ],JSON_UNESCAPED_UNICODE ));
        }


    }
    public function test_register(){
        return json_encode('php说过一句话');
    }
}
